from fastapi.responses import JSONResponse
import requests

class StarAPI:
    def __init__(self):
        self.starChinese = {
            "aquarius": "水瓶座",
            "pisces": "双鱼座",
            "aries": "白羊座",
            "taurus": "金牛座",
            "gemini": "双子座",
            "cancer": "巨蟹座",
            "leo": "狮子座",
            "virgo": "处女座",
            "libra": "天秤座",
           "scorpio": "天蝎座",
           "sagittarius": "射手座",
           "capricorn": "摩羯座"  
        }
    def handle(self, star: str):
        try:
            if not star:
                return JSONResponse(content={"code": 400, "msg": "请输入星座名称"}, status_code=3, media_type="application/json, charset=utf-8")
            if star.lower() not in self.starChinese:
                return JSONResponse(content={"code": 400, "msg": "星座名称错误"}, status_code=3, media_type="application/json, charset=utf-8")
            response = requests.get("https://xzdx.top/api/star", params={"star": star.lower()})
            data = response.json()
            if data["data"]:
                resData = {
                    "code": 200,
                    "msg": "获取成功",
                    "method": "GET",
                    "ip": data["ip"],
                    "data": data["data"],
                    "date": data["date"],
                    "parm": data["parm"]
                }
                return JSONResponse(content=resData, status_code=200, media_type="application/json, charset=utf-8")
            return JSONResponse(content={"code": 0, "msg": "获取数据失败，接口可能已经过期"}, status_code=200, media_type="application/json, charset=utf-8")
        except Exception as e:
            return JSONResponse(content={"code": 0, "msg": "获取失败", "error": str(e)}, status_code=5, media_type="application/json, charset=utf-8")